package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param request  将员工信息存入session
     * @param employee
     * @return
     */
    @PostMapping("/login") //获取json 数据
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        /**
         * 1、页面提交的密码 md5加密
         * 2、根据页面提交的username查询数据库
         * 3、如果没有查询到直接返回登录失败结果
         * 4、密码对比，如果不一致返回登录失败结果
         * 5、查看员工状态，如果为已禁用状态，则返回员工已禁用
         * 6、登录成功，将员工id 存入session并返回登录成功结果
         */

        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);
        if (one == null) {
            return Result.error("账号错误");
        }

        if (!one.getPassword().equals(s)) {
            return Result.error("密码错误");
        }

        if (one.getStatus() == 0) {
            return Result.error("账号已禁用");
        }

        request.getSession().setAttribute("employee", one.getId());
        return Result.success(one);

    }

    /**
     * 员工退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
//        request.getSession().removeAttribute("removeAttribute");
        request.getSession().removeAttribute("employee");
        Result<String> result = Result.success("退出成功");

        return result;

    }

    @PostMapping
    public Result<String> save(HttpServletRequest request, @RequestBody Employee employee) {

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(user1);
//        employee.setUpdateUser(user1);


        Long user1 = (Long) request.getSession().getAttribute("employee");


        employeeService.save(employee);
        return Result.success("新增员工成功");
    }


    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String name) {
//        log.info("page = {},pageSize={},name = {}",page,pageSize,name);
        // 构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询
        employeeService.page(pageInfo, queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 根据id 修改员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public Result<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        long id = Thread.currentThread().getId();
        log.info("update id = {}",id);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return Result.success("员工信息成功");
    }

    /**
     * 数据回显
     *
     * @return
     */
    @GetMapping("{id}")
    public Result getById(@PathVariable Long id) {
        Employee byId = employeeService.getById(id);

        return Result.success(byId);

    }
}
